package com.se.factory;

import io.micrometer.observation.ObservationRegistry;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class ChatModelFactory {
    private static final Map<String, ChatModel> chatModels = new ConcurrentHashMap<>();

    public ChatModel getOrCreate(String apiId, String apiKey, String url, String modelType, double temperature) {
        return chatModels.computeIfAbsent(apiId, key -> {
            OpenAiChatOptions options = new OpenAiChatOptions();
            options.setModel(modelType);
            options.setTemperature(temperature);

            return new OpenAiChatModel(
                    OpenAiApiFactory.getApi(url, apiKey),
                    options,
                    ToolCallingManager.builder().build(),
                    new RetryTemplate(),
                    ObservationRegistry.NOOP
            );
        });
    }
    public void remove(String ApiId){
        chatModels.remove(ApiId);
    }
}
